package com.zy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.mapper.PermissionMapper;
import com.zy.mapper.RolePermissionMapper;
import com.zy.pojo.Permission;
import com.zy.pojo.RolePermission;
import com.zy.service.PermissionService;
import com.zy.vo.TreeNode.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("permissionService")
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

  @Resource
  private RolePermissionMapper rolePermissionMapper;
   private List<TreeNode> getnodes(Integer pid, Integer roleId){
       List<Integer> perids =rolePermissionMapper.getPerid(roleId);
       List<TreeNode> nodes =new ArrayList<>();
       List<Permission> list = getPermissions(pid);
       for (Permission permission :list){
           TreeNode treeNode =new TreeNode();
           treeNode.setId(permission.getId());
           treeNode.setTitle(permission.getName());
           treeNode.setExpand(false);

           QueryWrapper queryWrapper1 =new QueryWrapper();
           queryWrapper1.eq("parent_id",permission.getId());
           Integer count =this.baseMapper.selectCount(queryWrapper1);
           if (perids.contains(permission.getId())&&count==0) {
               treeNode.setChecked(true);
           }
           nodes.add(treeNode);
       }
       return nodes;
   }

    private List<Permission> getPermissions(Integer pid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(pid==null){
            queryWrapper.isNull("parent_id");
        }else {
            queryWrapper.eq("parent_id",pid);
        }
        return super.list(queryWrapper);
    }

    @Override
    public List<TreeNode> getTreeNodes(Integer roleId) {
    //一级
        List<TreeNode> nodes1 =this.getnodes(null,roleId);
        for (TreeNode node1:nodes1){
            //二级
            List<TreeNode> nodes2 =this.getnodes(node1.getId(),roleId);
            node1.setChildren(nodes2);
            for (TreeNode node2:nodes2){
                List<TreeNode> nodes3 =this.getnodes(node2.getId(),roleId);
                node2.setChildren(nodes3);
            }
        }
        return nodes1;
    }

    @Override
    public void dogrant(Integer roleId, Integer[] nid) {
        rolePermissionMapper.deleteById(roleId);
        for (Integer perid:nid){
            rolePermissionMapper.insert(new RolePermission(roleId,perid));
        }
    }
    int count=0;
    @Override
    public List<String> getPerCodes(Integer roleId) {
        log.info("--------执行次数"+(++count));

       return baseMapper.getPer(roleId);
    }

    @Override
    public List<Permission> getPerM() {
        Subject subject= SecurityUtils.getSubject();
        //一级栏目
        List<Permission> menus01=this.getPermissions(null);
        menus01 =menus01.stream().filter(permission -> subject.isPermitted(permission.getPermCode())).collect(Collectors.toList());
        for(Permission p01:menus01){
            //二级菜单栏目
            List<Permission> menus02 =this.getPermissions(p01.getId());
            menus02 =menus02.stream().filter(permission -> subject.isPermitted(permission.getPermCode())).collect(Collectors.toList());
            p01.setChildren(menus02);
        }
        return menus01;
    }



}
